package com.cheboohui.iqs.taiping.direct.e;

import com.alibaba.fastjson.JSONObject;
import com.cheboohui.iqs.config.Config;
import com.cheboohui.iqs.taiping.direct.DirectTp;
import com.cheboohui.iqs.taiping.direct.PublicNew;
import com.cheboohui.iqs.taiping.direct.RequestHead;
import com.cheboohui.iqs.taiping.direct.ResTaiPing;

/**
 * E01城市列表查询请求类
 * 
 * @author xielonghai
 * @version 2016-06-30
 */
public class E01Request {

	/**
	 * E01城市列表查询
	 * 
	 * @param session
	 * @param searchTxt
	 * @param page
	 * @param count
	 * @return
	 */
	public static ResTaiPing getCityNews(String session, String searchTxt, int page, int count) {
		return requestE01(session, searchTxt, page, count);
	}

	/**
	 * E01城市列表查询
	 * 
	 * @param session
	 * @param searchTxt
	 * @return
	 */
	public static ResTaiPing getCityNews(String session, String searchTxt) {
		return requestE01(session, searchTxt, PublicNew.PAGE, PublicNew.COUNT);
	}

	/**
	 * 辅助接口-城市列表查询
	 * 
	 * @param session
	 * @param searchTxt
	 * @param page
	 * @param count
	 * @return
	 */
	private static ResTaiPing requestE01(String session, String searchTxt, int page, int count) {
		String value = Config.value + ":辅助接口-城市信息:" + searchTxt + ",查询城市code";
		String[] key = { "backCityMsg" };
		return DirectTp.request(value, PublicNew.REQUEST_URL + "/cityList", getRequest(session, searchTxt, page, count),
				key);
	}

	/**
	 * 获取--E01-城市列表查询------请求参数
	 * 
	 * @param searchTxt
	 *            城市名称/城市字母缩写
	 * @param page
	 *            页数
	 * @param count
	 *            每页条数(默认20条)
	 * @return
	 */
	public static String getRequest(String session, String searchTxt, int page, int count) {
		JSONObject head = RequestHead.getHead(session);
		String request = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
		request = request + "\n" + "<request>";
		request = request + "\n" + "<head>";

		request = request + "\n\t" + "<msgId>" + head.getString("msgId") + "</msgId>";
		request = request + "\n\t" + "<timeStamp>" + head.getString("timeStamp") + "</timeStamp>";
		request = request + "\n\t" + "<session>" + head.getString("session") + "</session>";
		request = request + "\n\t" + "<userCode>" + head.getString("userCode") + "</userCode>";

		request = request + "\n" + "</head>";
		request = request + "\n" + "<body>";

		request = request + "\n\t" + "<cityMsg>";
		request = request + "\n\t\t" + "<searchTxt>" + searchTxt + "</searchTxt>";
		request = request + "\n\t\t" + "<page>" + page + "</page>";
		request = request + "\n\t\t" + "<count>" + count + "</count>";
		request = request + "\n\t" + "</cityMsg>";

		request = request + "\n" + "</body>";
		request = request + "\n" + "</request>";
		return request;
	}
}
